package ai.fzy.demo.controller;

import ai.fzy.demo.pojo.config.Config;
import ai.fzy.demo.pojo.sysInfo.SystemInfo;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class DemoController {

    private static final Config CONFIG;

    static {
        CONFIG = Config.builder()
                .duration(TimeUnit.SECONDS.toMillis(5))
                .cfgDuration(TimeUnit.SECONDS.toMillis(5))
                .scripts(Map.of())
                .build();
    }

    /** 修改配置
     POST http://localhost:8080/config
     Content-Type: application/json

     {
        "cfgDuration": 1000,
        "duration": 1000
     }
     * */
    @PostMapping("/config")
    public void getConfig(@RequestBody Config config) {
        CONFIG.setDuration(config.getDuration());
        CONFIG.setCfgDuration(config.getCfgDuration());
        CONFIG.setScripts(config.getScripts());
    }

    /** 客户端获取配置 */
    @GetMapping("/config")
    public Config getConfig() {
        return CONFIG;
    }

    /** 客户端上传信息 */
    @PostMapping("/upload/{clientId}")
    @CrossOrigin(originPatterns = "*")
    public void uploadSystemInfo(@PathVariable String clientId, @RequestBody SystemInfo systemInfo) {
        //记录日志 or 写入数据库
        System.out.println(clientId + ":" +systemInfo);
    }
}
